import { createRouter, createWebHistory,createWebHashHistory } from 'vue-router'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect:'/home',
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('../views/home/index.vue')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login/index.vue')
    },
    {
      path: '/user',
      name: 'user',
      component: () => import('../views/user/index.vue')
    },
  ]
})
//防跳墙
router.beforeEach((to, from) => {
  if (to.fullPath === "/login") {
    if (localStorage.getItem("userinfo")) {
      return { path: "/home" };
    }
    return true;
  } else if (localStorage.getItem("userinfo")) {
    return true;
  } else {
    return { path: "/login" };
  }
});
export default router
